/**
 * Created by xiangyufang on 2016/12/1.
 */
;(function ($, window, document, undefined) {
    //角色名称
    var $name = $('#JQ_name');
    //角色编号
    var $number = $('#JQ_number');
    //表格
    var $table = $('#JQ_table');
    var DTTABLE;
    $table.checkbox3();


    /***新建编辑*/
    //编辑Id
    var $ne_id = $('#JQ_ne_id');
    //角色名称JQ_ne_id
    var $ne_name = $('#JQ_ne_name');
    //角色编号
    var $ne_number = $('#JQ_ne_number');
    //备注
    var $ne_remark = $('#JQ_ne_remark');
    //新建框
    var $action_form = $('#JQ_action_form');
    /***新建编辑*/

    /***授权信息*/
    //编辑Id
    var $s_id = $('#JQ_authorize_id');
    //授权信息表格
    var $s_table = $('#JQ_authorize_table');
    //授权框
    var $s_form = $('#JQ_authorize_div');
    /**授权信息**/


    //条件
    var filter = {
        //表格查询
        getSearch: function () {
            var filter = {};
            // 名称查询参数
            filter.name = function () {
                var v = $name.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            // 编号查询参数
            filter.code = function () {
                var v = $number.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            return filter;
        },

        //表格查询
        getSList: function () {
            var filter = {};
            // 名称查询参数
            filter.roleId = function () {
                var v = $s_id.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            return filter;
        },
        //新建&编辑
        getNewEdit: function () {
            var filter = {};
            // id
            filter.id = function () {
                var v = $ne_id.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            // 名称
            filter.name = function () {
                var v = $ne_name.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            // 编号
            filter.code = function () {
                var v = $ne_number.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            // 备注
            filter.remark = function () {
                var v = $ne_remark.val();
                if (v) {
                    return v;
                }
                return "";
            }();

            return filter;
        },
        //多选ID数组
        getIdArr: function(){
            var arr = [];
            var $chk = $table.find('i.zy-checkbox');
            if($chk.length > 0){
                $chk.each(function(){
                    var $this = $(this);
                    if($this.hasClass('active')){
                        var value = $this.attr('data-id');
                        arr.push(value);
                    }
                });
            }
            return arr;
        },
        //多选ID数组
        getSIdArr: function(){
            var filter = {};
            //id
            filter.id = function(){
                var v = $s_id.val();
                if (v) {
                    return v;
                }
                return "";
            }();
            filter.menuIds = function(){
                var arr = [];
                var $chk = $s_table.find('i.xyf-checkbox');
                if($chk.length > 0){
                    $chk.each(function(){
                        var $this = $(this);
                        if($this.hasClass('active') || $this.hasClass('half')){
                            arr.push($this.attr('data-id'));
                        }
                    });
                }
                return arr;
            }();
            return filter;
        }
    };


    DTTABLE = $table.DataTable({
        'language': {
            'zeroRecords': DyyBase.table_empty()
        },
        'info': false,
        'paging': false,
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': false,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch();
            DyyBase.ajax(sysApi.url_settings_role_query(), param, function(dataSet) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.data = DyyBase.clearNull(dataSet.result);
                callback(returnData);
            });
        },
        'columns': [
            {
                data: 'id',
                render: function (data) {
                    return '<i class="zy-checkbox" data-id="' + data + '" title="选中"></i>';
                }
            },
            {
                data: 'name',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'code',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'remark',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'state',
                render: function (data,type,row) {
                    if(data == '0'){
                        return '<i data-id="'+row.id+'" data-state="'+data+'" class="zy-switch-sys"></i>';
                    }else{
                        return '<i data-id="'+row.id+'" data-state="'+data+'" class="zy-switch-sys off"></i>';
                    }
                }
            },
            {
                data: 'id',
                className : 't-r',
                render: function (data,type,row) {
                    if(row.state == '0'){
                        return '<i data-id="'+data+'" class="zy-record-add-no-icon"></i>';
                    }else{
                        return '<i data-id="'+data+'" class="zy-record-add-icon" title="编辑"></i>';
                    }
                }
            },
            {
                data: 'id',
                render: function (data,type,row) {
                    if(row.state == '0'){
                        return '<i data-id="'+data+'" class="zy-list-icon-no-delete"></i>';
                    }else{
                        return '<i data-id="'+data+'" class="zy-list-icon-delete" title="删除"></i>';
                    }
                }
            },
            {
                data: 'id',
                className : 't-l',
                render: function (data) {
                    return '<i data-id="'+data+'" class="xyf-shouquan-icon" title="授权"></i>';
                }
            }
        ]
    });

    //查询
    $('#JQ_search').click(function(){
        if(DTTABLE){
            DTTABLE.draw();
        }
    });


    // 新建
    $('#JQ_role_create').click(function(){
        $(this).WIND({
            width: 750,
            height: 350,
            title: '新建',
            actionBox: 'div#JQ_ne_div',
            yesBtnAct: actionRole,
            resetForm: $action_form
        });
    });
    // 编辑
    $table.delegate('i.zy-record-add-icon','click',function(){
        var $this = $(this);
        var $tr = $this.parent('td').parent('tr');
        var data = DTTABLE.row($tr).data();
        if(data){
            // 填充数据
            $ne_id.val(data.id);

            // ps: this is important
            $ne_name.val(data.name);
            $ne_number.val(data.code);
            $ne_remark.val(data.remark);

            var wind = $this.WIND({
                width: 750,
                height: 350,
                title: '编辑',
                actionBox: 'div#JQ_ne_div',
                yesBtnAct: function(){
                    actionRole(wind.clear,data);
                },
                noBtnAct: function(){
                    $action_form.reset();
                    wind.clear();
                },
                closeBtnAct: function(){
                    $action_form.reset();
                    wind.clear();
                }
            });
        }
    });

    // 新建 / 编辑
    function actionRole(callback,data){
        var verify = $action_form.verify(), title = '';
        if(verify === true){
            if(data){
                title = '编辑';
            }else{
                title = '新建';
            }
            var param = filter.getNewEdit();
            if(callback instanceof Function){
                callback();
            }
            DyyBase.ajax(sysApi.url_settings_role_new(),param,function(dataSet){
                if(dataSet.bool){
                    $(this).MSG({
                        type: 'success',
                        content: title+'成功!',
                        yesBtnAct: function(){
                            if(DTTABLE){
                                DTTABLE.draw();
                            }
                        },
                        closeBtnAct: function(){
                            if(DTTABLE){
                                DTTABLE.draw();
                            }
                        }
                    });
                    $action_form.reset();
                }
            })
        }
    }

    // 删除
    $table.delegate('i.zy-list-icon-delete','click',function(){
        var $this = $(this);
        var id = $this.attr('data-id');
        id = [id];
        var param = id;
        $this.MSG({
            type: 'confirm',
            content: '您确认要删除此条信息吗?',
            yesBtnTxt: '删除',
            height: 230,
            yesBtnAct: function(){
                DyyBase.ajax(sysApi.url_settings_role_delete(),DyyBase.convertToArr({ids:param}),function(dataSet){
                    if(dataSet.bool){
                        $this.MSG({
                            type: 'success',
                            content: '删除成功!',
                            yesBtnAct: function(){
                                if(DTTABLE){
                                    DTTABLE.draw();
                                }
                            },
                            closeBtnAct: function(){
                                if(DTTABLE){
                                    DTTABLE.draw();
                                }
                            }
                        });
                    }
                });
            }
        });
    });

    // 批量删除
    $('#JQ_role_delete').click(function(){
        var $this = $(this);
        var param = filter.getIdArr();
        if(param.length > 0){
            $this.MSG({
                type: 'confirm',
                height: 230,
                content: '您确认要删除选中的角色信息?',
                yesBtnTxt: '删除',
                yesBtnAct: function(){
                    DyyBase.ajax(sysApi.url_settings_role_delete(),DyyBase.convertToArr({ids:param}),function(dataSet){
                        if(dataSet.bool){
                            $this.MSG({
                                type: 'success',
                                content: '删除成功!',
                                yesBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                },
                                closeBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }else{
            $this.MSG({
                content: '请选择要删除的角色信息!'
            });
        }
    });

    // 启用&停用
    $table.delegate('i.zy-switch-sys','click',function(){
        var $this = $(this);
        var id = $this.attr('data-id');
        id = [id];
        var state = $this.attr('data-state');
        var param = {};
        param.ids = id;
        if(state == '0'){
            param.state = '1';

        }else{
            param.state = '0';
        }
        DyyBase.ajax(sysApi.url_settings_role_enable(),DyyBase.convertToArr(param),function(dataSet){
            if(dataSet.bool){
                if(DTTABLE){
                    DTTABLE.draw();
                }
            }
        });
    });

    //批量启用
    $('#JQ_role_enable').click(function(){
        var $this = $(this);
        var param = {};
        param.ids= filter.getIdArr();
        param.state = '0';
        if(param.ids.length > 0){
            $this.MSG({
                type: 'confirm',
                height: 230,
                content: '您确认要启用选中的角色信息?',
                yesBtnTxt: '确定',
                yesBtnAct: function(){
                    DyyBase.ajax(sysApi.url_settings_role_enable(),DyyBase.convertToArr(param),function(dataSet){
                        if(dataSet.bool){
                            $this.MSG({
                                type: 'success',
                                content: '启用成功!',
                                yesBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                },
                                closeBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }else{
            $this.MSG({
                content: '选择需要启用的数据!'
            });
        }
    });

    //批量停用
    $('#JQ_role_disable').click(function(){
        var $this = $(this);
        var param = {};
        param.ids= filter.getIdArr();
        param.state = '1';
        if(param.ids.length > 0){
            $this.MSG({
                type: 'confirm',
                height: 230,
                content: '您确认要停用选中的角色信息?',
                yesBtnTxt: '确认',
                yesBtnAct: function(){
                    DyyBase.ajax(sysApi.url_settings_role_enable(),DyyBase.convertToArr(param),function(dataSet){
                        if(dataSet.bool){
                            $this.MSG({
                                type: 'success',
                                content: '停用成功',
                                yesBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                },
                                closeBtnAct: function(){
                                    if(DTTABLE){
                                        DTTABLE.draw();
                                    }
                                }
                            });
                        }
                    });
                }
            });
        }else{
            $this.MSG({
                content: '选择需要停用的数据!'
            });
        }
    });

    // 加载表格
    var STABLE = $s_table.DataTable({
        'language': {
            'zeroRecords': DyyBase.table_empty()
        },
        'info': false,
        'paging': false,
        'scrollY': 410,
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSList();
            DyyBase.ajax(sysApi.url_settings_role_authorize(),param,function(dataSet){
                var returnData = {};
                returnData.draw = data.draw;
                var node = DyyBase.clearNull(dataSet.result);
                function cutData(data){
                    if(data.length>0){
                        for(var i=0;i<data.length;i++){
                            if(data[i].id == 'M0000000000000000000000000000000'){
                                data[i] = {cuteId:'0',id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth};
                            }else{
                                var u=data[i].id.substring(17,32);
                                var v=data[i].id.substring(20,32);
                                var w=data[i].id.substring(23,32);
                                var x=data[i].id.substring(26,32);
                                var y=data[i].id.substring(29,32);
                                if(u == '000000000000000'){
                                    data[i] = {cuteId:data[i].id.substring(14,17),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                }else{
                                    if(v=='000000000000'){
                                        data[i] = {cuteId:data[i].id.substring(14,20),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                    }else{
                                        if(w == '000000000'){
                                            data[i] = {cuteId:data[i].id.substring(14,23),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                        }else{
                                            if(x == '000000'){
                                                data[i] = {cuteId:data[i].id.substring(14,26),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                            }else{
                                                if(y == '000'){
                                                    data[i] = {cuteId:data[i].id.substring(14,29),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                                }else{
                                                    data[i] = {cuteId:data[i].id.substring(14,32),id:data[i].id,code:data[i].code,name:data[i].name,imgUrl:data[i].imgUrl,url:data[i].url,remark:data[i].remark,isAuth:data[i].isAuth}
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return data;
                }
                returnData.data = cutData(node);
                callback(returnData);
            },{async:false});
        },
        'columns': [
            {
                data: 'id',
                render: function (data,type,row) {
                    var c_id = row.cuteId;
                    if(c_id){
                        var dataStr = c_id.toString();
                        if(dataStr.length == 1){
                            return  '<i class="xyf-zero-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'"  data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length == 3){
                            return  '<i class="xyf-one-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'"  data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length ==6){
                            return '<i class="xyf-two-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'" data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length ==9){
                            return '<i class="xyf-three-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'" data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length ==12){
                            return  '<i class="xyf-four-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'" data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length ==15){
                            return '<i class="xyf-five-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'" data-type="check_'+ dataStr +'"></i>';
                        }else if(dataStr.length ==18){
                            return '<i class="xyf-six-checked"></i>' + '<i class="xyf-checkbox xyf-state'+ row.isAuth +'" title="选中" data-id="'+ data +'" data-type="check_'+ dataStr +'"></i>';
                        }else{
                            return '<span data-id="'+ data +'" title="错误数据">错误数据</span>'
                        }
                    }else{
                        return '<span data-id="'+ data +'" title="错误数据">错误数据</span>'
                    }
                }
            },
            {
                data: 'cuteId',
                render: function (data,type,row) {
                    if(data){
                        var dataStr = data.toString();
                        if(dataStr.length == 1){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-zero" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length == 3){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-one" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length ==6){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-two" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length ==9){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-three" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length ==12){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-four" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length ==15){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-five" data-type="level'+ data +'"></i>';
                        }else if(dataStr.length ==18){
                            return '<span title="' + row.name + '">' + row.name + '</span><i class="haveData xyf-six" data-type="level'+ data +'"></i>';
                        }else{
                            return '<span data-id="'+ data +'" title="错误数据">错误数据</span>'
                        }
                    }else{
                        return '<span data-id="'+ data +'" title="错误数据">错误数据</span>'
                    }
                }
            },
            {
                data: 'code',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'url',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            },
            {
                data: 'imgUrl',
                render: function (data) {
                    return '<span title="' + data + '">' + data + '</span>';
                }
            }
        ]
    });
    $s_table.checkNewScroll();



    // 授权authorize
    $table.delegate('i.xyf-shouquan-icon','click',function(){
        var $this = $(this);
        var $tId = $this.attr('data-id');
        $s_id.val($tId);
        if(STABLE){
            STABLE.draw();
            $s_table.addColor();
            //定义滚动表头的宽度
            $('div.dataTables_scrollHeadInner').css('width','100%');
            $('div.dataTables_scrollHeadInner').find('table').css('width','100%');
        }
        var wind = $this.WIND({
            width: 750,
            height: 560,
            title: '授权信息',
            actionBox: 'div#JQ_s_div',
            yesBtn: 'JQ_authorize_submit',        // 确认按钮
            yesBtnAct: function(){
                actionAuthorize(wind.clear,$this);
            },
            closeBtnAct: function(){
                $('#JQ_authorize_check_all').removeClass('half').removeClass('active');
                $('i.xyf-checkbox').removeClass('half').removeClass('active');
                wind.clear();
            }
        });
    });
    function actionAuthorize(callback,tag){
        var param = filter.getSIdArr();
        if(param.menuIds.length <= 0){
            tag.MSG({content: '请选择授权菜单!'});
        }else{
            DyyBase.ajax(sysApi.url_settings_authorize_submit(),DyyBase.convertToArr(param),function(dataSet){
                if(dataSet.bool){
                    tag.MSG({content: '授权成功!'});
                }
            });
            if(callback instanceof Function){
                callback();
            }
            $('#JQ_authorize_check_all').removeClass('half').removeClass('active');
            $('i.xyf-checkbox').removeClass('half').removeClass('active');
        }
    }

})(jQuery, window, document);